package com.app.piemoment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.piemoment.api.ApiResult;
import com.app.piemoment.api.ApiReturnResultListener;
import com.app.piemoment.api.MatchApi;
import com.app.piemoment.config.Config;
import com.app.piemoment.database.SettingLoader;
import com.app.piemoment.entity.MatchInfo;
import com.app.piemoment.entity.OppositeInfo;
import com.app.piemoment.entity.Photo;
import com.app.piemoment.image.ImageLoaderManager;
import com.app.piemoment.util.Utils;

public class MatchWaitActivity extends SlideBaseActivity {
	private TextView hour_text;
	private TextView min_text;
	private TextView sec_text;
	private ImageView opp_image;
	private ImageView personal_image;
	private Button choose_text;
	private TextView msg_text;
	private ImageView foot_image;
	private Timer timer;
	protected ArrayList<MatchInfo> list;
	private int mid;

	public MatchWaitActivity() {
		mainViewId = R.layout.match_wait;
	}

	@Override
	protected void initMainView() {
		initTitle(R.drawable.menu_bg, "PieMoment", 0,0);
		//时间
		hour_text = (TextView)findViewById(R.id.hour_text);
		min_text = (TextView)findViewById(R.id.min_text);
		sec_text = (TextView)findViewById(R.id.sec_text);
		
		//对方头像
		opp_image = (ImageView)findViewById(R.id.opp_image);
		choose_text = (Button)findViewById(R.id.choose_text);
		personal_image = (ImageView)findViewById(R.id.personal_image);
		msg_text = (TextView)findViewById(R.id.msg_text);
		foot_image = (ImageView)findViewById(R.id.foot_image);
	}
	

	@Override
	protected void registerMainViewEvent() {
		opp_image.setOnClickListener(this);
		personal_image.setOnClickListener(this);

	}

	@Override
	protected void processMainView() {
		getMatch();
		countDown();
	}

	
	private void countDown() {
		if(timer!=null) {
			timer.cancel();
			timer = null;
		}
		final Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)+1, 12, 0, 0);
		timer = new Timer();
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						
						long clock = cal.getTimeInMillis();
						long seconds = (clock-System.currentTimeMillis())/1000;
						int hour = (int) (seconds/60/60);
						int minut = (int) (seconds/60 - 60*hour);
						int sec = (int) (seconds - 60*minut - 60*60*hour);
						hour_text.setText(hour+"");
						min_text.setText(minut+"");
						sec_text.setText(sec+"");
						if(hour==0&&minut==0&&sec==0) {
							if(timer!=null) {
								timer.cancel();
								timer = null;
							}
							getMatch();
						}
					}
					
				});
				
			}
		}, 0, 1000);
	}

	private void getMatch() {
		MatchApi api = new MatchApi(this);
		api.setReturnResultListener(new ApiReturnResultListener() {
			
			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				if(ApiResult.RESULT_OK==apiResult.getResultCode()) {
					JSONObject jsonEntity = apiResult.getJsonEntity();
					Log.e("aa", jsonEntity.toString());
					try {
						if (jsonEntity != null) {
							if(jsonEntity.has("status")) {
								String result = jsonEntity.getString("status");
								if(ApiResult.Success.equals(result)) {
									if(jsonEntity.has("data")) {
										JSONArray array = jsonEntity.getJSONArray("data");
										if(array!=null&&array.length()>0) {
											if(list==null) {
												list = new ArrayList<MatchInfo>();
											}
											for(int i=0;i<array.length();i++) {
												JSONObject obj = (JSONObject) array.opt(i);
												MatchInfo info = new MatchInfo();
												info.parser(obj);
												list.add(info);
											}
										}
									}
									
									
								}
								if(list!=null&&list.size()>0) {
									updateUI(list.get(0));
								}
							}
						
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				
			}
			
			@Override
			public <T> void onReturnLoading(int requestCode) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public <T> void onReturnFailResult(int requestCode, ApiResult<T> apiResult) {
				// TODO Auto-generated method stub
				
			}
		});
		api.getMatchs(1, this);
		
		countDown();
	}
	
	protected void updateUI(MatchInfo matchInfo) {
		mid = matchInfo.getMid();
		OppositeInfo target_info = matchInfo.getTarget_info();
		if (target_info != null) {
			ArrayList<Photo> photos = target_info.getPhotos();
			if (photos != null && photos.size() > 0) {
				Photo photo = photos.get(0);
				ImageLoaderManager imageloader = new ImageLoaderManager(this,new Handler());
				opp_image.setImageBitmap(Utils.toRoundBitmap(imageloader.getImage(Config.IMAGE_URL + photo.getFilepath(),R.drawable.default_opp_icon, 0)));
			}
		}
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		super.onClick(v);
		switch (v.getId()) {
		case R.id.btn_left:
			super.clickSlide();
			break;
		case R.id.personal_image:
			intent = new Intent(this,PersonalActvitiy.class);
			intent.putExtra("mid", mid+"");
			startActivity(intent);
			break;
		case R.id.opp_image:
			if(mid!=0) {
				SettingLoader.setMatchId(this,mid);
				intent = new Intent(this,PairActivity.class);
				intent.putExtra("mid", mid+"");
				startActivity(intent);
			}
			break;
		default:
			break;
		}
	}
}
